package com.zhiyou100.crm.servlet.role;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhiyou100.crm.pojo.AdminBaseServlet;
import com.zhiyou100.crm.pojo.Role;
import com.zhiyou100.crm.service.RoleService;
import com.zhiyou100.crm.service.impl.RoleServiceImpl;

@WebServlet(urlPatterns = { "/role/add" })
public class AddServlet extends AdminBaseServlet {
	private static final long serialVersionUID = 1L;
	RoleService roleService=new RoleServiceImpl();

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/view/role/add.jsp").forward(request,response);
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		String roleName =request.getParameter("roleName");
		String roleDesc = request.getParameter("roleDesc");
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long l = System.currentTimeMillis();
		String format = df.format(new Date(l));
		Timestamp time=Timestamp.valueOf(format);
		Role role=new Role(roleName,roleDesc,time);
		int a=roleService.insertRole(role);
		if (a > 0) {
			response.sendRedirect(request.getContextPath() + "/role/list");
		}else {
			request.getRequestDispatcher("/WEB-INF/view/role/add.jsp").forward(request, response);
		}
	}
}
